
package data;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EditarCursosGateway {
    
    private Connection conexion;

    public EditarCursosGateway(){
        Conector conector = new Conector();
        conexion = conector.darConexion();
        Statement stmt;
        try {
            stmt = conexion.createStatement();
            stmt.execute("use be");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public ResultSet obtenerCursos(){
        Statement stmt;
        ResultSet rs = null;
        try {
            stmt = conexion.createStatement();
            rs = stmt.executeQuery("Select sigla, nombre from Curso");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
        return rs;
    }
    
    public ResultSet obtenerCurso(String sigla){
        Statement stmt;
        ResultSet rs = null;
        try {
            stmt = conexion.createStatement();
            rs = stmt.executeQuery("Select nombre, creditos from Curso where sigla=\'"+sigla+"\'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
        return rs;
    }
    
    public void agregarCurso(String sigla, String nombre, String creditos){
        Statement stmt;
        try {
            stmt = conexion.createStatement();
            stmt.execute("insert into curso (sigla,nombre,creditos) values (\'"+sigla+"\',\'"+nombre+"\',"+creditos+")");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public void actualizarCurso(String sigla, String nombre, String creditos){
        Statement stmt;
        try {
            stmt = conexion.createStatement();
            stmt.executeUpdate("Update Curso Set nombre= \'"+nombre+"\',creditos= \'"+creditos+"\' Where sigla = \'"+sigla+"\'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public String obtenerRequisitos(String sigla){
        String requisitos = "";
        Statement stmt;
        ResultSet rs = null;
        try {
            stmt = conexion.createStatement();
            rs = stmt.executeQuery("Select SiglaCursoRequisito from dependenciascursos where SiglaCurso=\'"+sigla+"\'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        try {
            while(rs.next()){
                requisitos += rs.getString(1) + " ";
            }
        } catch (SQLException ex) {
            Logger.getLogger(EditarCursosGateway.class.getName()).log(Level.SEVERE, null, ex);
        }
        return requisitos;
    }
    
    public void agregarRequisito(String sigla, String siglaR){
        Statement stmt;
        try {
            stmt = conexion.createStatement();
            stmt.execute("insert into dependenciascursos (SiglaCurso,SiglaCursoRequisito) values (\'"+sigla+"\',\'"+siglaR+"\')");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public void eliminarCurso(String sigla){
        Statement stmt;
        try {
            stmt = conexion.createStatement();
            stmt.execute("delete from curso where sigla = \'"+sigla+"\'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
